//车辆主营线路管理
interface ICarRouteManagentViewModel extends IBaseViewModel<ICarRouteManagentQueryTerms> {
    /**
     * 是否进行验证(不验证为-1)
     */
    required:boolean;
    ischecked:boolean;
    /**
     * 列表高
     */
    heightA:number;
}


interface ICarRouteManagentQueryTerms {

}

class CarRouteManagentController extends ListController<ICarRouteManagentViewModel, ICarRouteManagentQueryTerms> {
    gridApi: uiGrid.IGridApiOf<RouteLineListItemResponse>;
    carId: string;

    constructor($scope: ICarRouteManagentViewModel,private areaService:IAreaService,private carService:ICarService,  private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        /**
         * 车辆id
         */
        this.carId = JSON.parse(window.localStorage.getItem("car").toString()).carId;
        if(!this.$scope.required){
            this.$scope.startProvinceCode="-1";
            this.$scope.startCityCode = "-1";
            this.$scope.endProvinceCode = "-1";
            this.$scope.endCityCode = "-1";
        };
        /**
         * 获取省下拉
         */
        this.areaService.getArea().then(res =>{
            this.$scope.provinceList = res.data.data;
        });
        this.$scope.changeStarProvince = this.changeStartProvince;
        this.$scope.changeStartCity = this.changeStartCity;
        this.$scope.changeEndProvince = this.changeEndProvince;
        this.$scope.changeEndCity = this.changeEndCity;

        this.$scope.gridOptions = {
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "承运起始地", field: 'beginAddress', enableColumnMenu: false },
                { displayName: "承运目的地", field: 'endAddress', minWidth: 300, enableColumnMenu: false },
                { displayName: "生成日期", field: 'createTime', enableColumnMenu: false }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
    }

    loadData(queryTerms: ICarRouteManagentQueryTerms, skip: number, count: number): void {
        this.carService.getDetail(this.carId).then((res)=>{
            this.$scope.gridOptions.data = res.data.routeList;
            this.$scope.heightA = (this.$scope.gridOptions.data.length * 39)+95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        })
    }
    /**
     * 改变起始地省
     */
    changeStartProvince = ()=>{
        if(!this.$scope.startProvinceCode||this.$scope.startProvinceCode ==""){
            this.$scope.startCityList.length=0;
            if(!this.$scope.required){
                this.$scope.startProvinceCode="-1";
                this.$scope.startCityCode = "-1";
            };
            return;
        };
        this.areaService.getCity(this.$scope.startProvinceCode).then(res=>{
            this.$scope.startCityList = res.data.data;
        });
    }
    /**
     * 改变起始地市
     */
    changeStartCity = ()=>{
        if(!this.$scope.startCityCode||this.$scope.startCityCode ==""){
            if(!this.$scope.required){
                this.$scope.startCityCode="-1";
            };
        };
    }

    /**
     * 改变目的地省
     */
    changeEndProvince = ()=>{
        if(!this.$scope.endProvinceCode||this.$scope.endProvinceCode ==""){
            this.$scope.endCityList.length=0;
            if(!this.$scope.required){
                this.$scope.endProvinceCode = "-1";
                this.$scope.endCityCode = "-1";
            };
            return;
        };
        this.areaService.getCity(this.$scope.endProvinceCode).then(res=>{
            this.$scope.endCityList = res.data.data;
        });
    }
    /**
     * 改变起始地市
     */
    changeEndCity = ()=>{
        if(!this.$scope.endCityCode||this.$scope.endCityCode ==""){
            if(!this.$scope.required){
                this.$scope.endCityCode="-1";
            };
        };
    }


}

angular.module("tms").controller("carRouteManagentController", ["$scope","areaService", "carService", "$location", CarRouteManagentController]);